package app.esb.disk;

import app.esb.common.NumberTools;
import app.portal.util.Arith;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;

/**
 * 磁盘信息处理类
 * @author JiaJ
 *
 */
public class DiskExcutor {
	
	private String diskID;
	
	private double diskSize;
	
	private double diskUse;
	
	private double diskFree;
	
	private double diskPer;
	
	public DiskExcutor(){}
	
	/**
	 * 初始化磁盘
	 * @param diskID
	 */
	public DiskExcutor(String diskID){
		
		this.diskID = diskID;
		
		DBManager dbManager = DBFactory.getActionDB();
		DataTable dt = dbManager.executeQueryTable("select * from ${schema}.LEO_DISK_INFO where DISK_ID='"+diskID+"'");
		
		if(dt.next()){
			
			this.diskSize = dt.getDouble("DISK_ACTIVE_SIZE");
			this.diskUse = dt.getDouble("DISK_ACTIVE_USE");
			this.diskFree = diskSize - diskUse;
			
		}
		
	}
	
	/**
	 * 初始化容量
	 */
	public void instSize(){
		
		this.diskUse = 0.00;
		this.diskFree = this.diskSize;
		
		DBManager dbManager = DBFactory.getActionDB();
		DataTable dt = dbManager.executeQueryTable("select * from ${schema}.LEO_RES_INFO where DISK_ID='"+diskID+"'");
		
		while(dt.next()){
			this.addSize(dt.getString("RES_STORE_SIZE"));
		}
		
		this.diskPer = Arith.round(this.diskUse / this.diskSize, 4) * 100;
	
	}
	
	/**
	 * 添加容量
	 * @param addSize
	 */
	public void addSize(String addSize){
		
		double activeSize = NumberTools.toKBSize(addSize);
		
		this.diskUse += activeSize;
		this.diskFree = diskSize - diskUse;
		
		this.diskPer = Arith.round(this.diskUse / this.diskSize, 4) * 100;
				
	}
	
	/**
	 * 减少容量
	 * @param delSize
	 */
	public void delSize(String delSize){
		
		double activeSize = NumberTools.toKBSize(delSize);
		
		this.diskUse -= activeSize;
		this.diskFree = diskSize - diskUse;
		
		this.diskPer = Arith.round(this.diskUse / this.diskSize, 4) * 100;
			
	}
	
	/**
	 * 操作数据库
	 */
	public void excute(){
		
		String useStr = NumberTools.toSizeString(this.diskUse);
		String freeStr = NumberTools.toSizeString(this.diskFree);
		
		DBManager dbManager = DBFactory.getActionDB();
		dbManager.executeUpdate("update ${schema}.LEO_DISK_INFO "
				              + "set DISK_USE_SIZE='"+useStr+"',DISK_FREE_SIZE='"+freeStr+"',DISK_PERSENT='"+NumberTools.scale(this.diskPer,2)+"',DISK_ACTIVE_USE='"+NumberTools.scale(diskUse,2)+"' "
				              + "where DISK_ID='"+this.diskID+"'");
	}
	
}
